package com.joy.ogljoy_01.opengl.util;

public class Utils {

	/**
	 * Find the smallest power of two >= the input value. (Doesn't work for
	 * negative numbers.)
	 */
	public static int roundUpPower2(int x) {
		x = x - 1;
		x = x | (x >> 1);
		x = x | (x >> 2);
		x = x | (x >> 4);
		x = x | (x >> 8);
		x = x | (x >> 16);
		
		return x + 1;
	}
	
	public static int[] int2Array(int x) {
		int[] a = null;
		a = new int[getIntLength(x)];
		for (int i = a.length; i > 0; i--) {
			a[i - 1] = x % 10;
			x /= 10;
		}
		return a;
	}

	public static String getStringByNumber(int num, int length) {
		StringBuffer sb = new StringBuffer();
		int count = getIntLength(num);
		for (int i = length; i > 0; i--) {
			if (count < i) {
				sb.append("0");
			} else {
				sb.append(num);
				break;
			}
		}
		return sb.toString();
	}

	public static int getIntLength(int x) {
		int length = 1;
		while (x / 10 > 0) {
			x /= 10;
			length++;
		}
		return length;
	}
	
}
